package com.example.spring_xx_u_market;

import com.alibaba.fastjson.JSONObject;
import com.example.bean.UserInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import util.UidCtrl;
import util.UtilTime;
import util.utilmysql.UtilDruid;

import javax.servlet.http.HttpServletRequest;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;

@Slf4j
@Controller
@RequestMapping("api")
public class UploadDemand {
    @PostMapping("upload_demand")
    public String setCommodityInformation(HttpServletRequest request){
        String account = request.getParameter("account");
        String uid = request.getParameter("uid");
        if(uid == null){
            return "008";//非法访问，没有传递uid给服务器
        }
        JdbcTemplate jdbcTemplate = new JdbcTemplate(UtilDruid.getDateSource());
        List<UserInfo> query = jdbcTemplate.query("select * from user_info where account=?",
                new BeanPropertyRowMapper<UserInfo>(UserInfo.class), account);
        String authAccount;
        authAccount = UidCtrl.checkUid(uid);
        if(authAccount.compareTo(account) != 0){
            return "009";//登录认证失效，需重新登录
        }
        JSONObject goodsInfo = JSONObject.parseObject(request.getParameter("demands_info"));
        String name = goodsInfo.getString("name");
        int priceId;
        double lowPrice = goodsInfo.getDouble("low_price");
        double highPrice = goodsInfo.getDouble("high_price");
        String detailInfo = goodsInfo.getString("detail_info");
        String goodsType = goodsInfo.getString("goods_type");
        int timeInfoId;
        //String startTime = goodsInfo.getString("start_time");
        String startTime = UtilTime.getStringTimeNow();
        String endTime = goodsInfo.getString("end_time");
        final String sql = "insert into goods_info(name,type,price_info_id,detail_info,goods_type,time_info_id,state,account) value (?,demand,?,?,?,?,?,?)";
        final String sql1 = "insert into second_hand.price_info(low,high) value (?,?)";
        final String sql2 = "insert into time_info(start_time,end_time) value (?,?)";
        //jdbcTemplate.update("insert into price_info(low,high) value (?,?)",lowPrice,highPrice);
        KeyHolder keyHolder = new GeneratedKeyHolder();
        jdbcTemplate.update(new PreparedStatementCreator() {
            public PreparedStatement createPreparedStatement(Connection con) throws SQLException
            {
                PreparedStatement ps = jdbcTemplate.getDataSource()
                        .getConnection().prepareStatement(sql1, Statement.RETURN_GENERATED_KEYS);
                ps.setDouble(1,lowPrice);
                ps.setDouble(2,highPrice);
                return ps;
            }
        }, keyHolder);
        priceId = keyHolder.getKey().intValue();
        jdbcTemplate.update(new PreparedStatementCreator() {
            public PreparedStatement createPreparedStatement(Connection con) throws SQLException
            {
                PreparedStatement ps = jdbcTemplate.getDataSource()
                        .getConnection().prepareStatement(sql2,Statement.RETURN_GENERATED_KEYS);
                ps.setString(1,startTime);
                ps.setString(2,endTime);
                return ps;
            }
        }, keyHolder);
        timeInfoId = keyHolder.getKey().intValue();
        jdbcTemplate.update(sql,name,priceId,detailInfo,goodsType,timeInfoId,1,account);
        return "202";
    }
}
